<template>
  <div class="forum-category-view">
    <!-- 页面标题 -->
    <div class="page-header">
      <div class="header-content">
        <div class="title-section">
          <h1 class="page-title">论坛分类</h1>
          <p class="page-subtitle">选择感兴趣的分类浏览相关内容</p>
        </div>
      </div>
    </div>

    <!-- 分类网格 -->
    <div class="categories-grid">
      <el-card 
        v-for="category in categories" 
        :key="category.id" 
        class="category-card" 
        @click="navigateToCategory(category)"
        shadow="hover"
      >
        <div class="category-content">
          <div class="category-icon">
            <el-icon :size="48"><Folder /></el-icon>
          </div>
          <div class="category-info">
            <h3 class="category-name">{{ category.name }}</h3>
            <p class="category-description">{{ category.description || '暂无描述' }}</p>
            <div class="category-stats">
              <div class="stat-item">
                <el-icon><Document /></el-icon>
                <span>{{ category.postCount || 0 }} 帖子</span>
              </div>
            </div>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <el-skeleton :rows="6" animated />
    </div>

    <!-- 空状态 -->
    <div v-else-if="categories.length === 0" class="empty-container">
      <el-empty description="暂无分类数据" />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Folder, Document } from '@element-plus/icons-vue'
import { getCategories } from '@/api/forum'
import type { Category } from '@/types/forum'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const categories = ref<Category[]>([])

// 模拟分类数据（作为API失败时的回退）
const mockCategories = ref<Category[]>([
  {
    id: 1,
    name: '行测',
    description: '行政职业能力测验相关讨论',
    postCount: 156,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 2,
    name: '申论',
    description: '申论写作技巧和经验分享',
    postCount: 89,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 3,
    name: '面试',
    description: '公务员面试准备和技巧',
    postCount: 67,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 4,
    name: '经验分享',
    description: '上岸经验分享和备考心得',
    postCount: 234,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 5,
    name: '资料分享',
    description: '学习资料和备考资源分享',
    postCount: 45,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 6,
    name: '答疑解惑',
    description: '学习过程中的疑问解答',
    postCount: 78,
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  }
])

// 加载分类数据
const loadCategories = async () => {
  try {
    loading.value = true
    console.log('加载分类数据...')
    
    const response = await getCategories()
    console.log('API响应:', response)
    
    if (response && response.length > 0) {
      categories.value = response
    } else {
      // 使用模拟数据
      categories.value = mockCategories.value
    }
    
    console.log('分类加载完成:', categories.value.length)
    
  } catch (error) {
    console.error('加载分类失败:', error)
    ElMessage.error('加载分类失败，使用模拟数据')
    
    // 使用模拟数据作为回退
    categories.value = mockCategories.value
  } finally {
    loading.value = false
  }
}

// 导航到分类
const navigateToCategory = (category: Category) => {
  console.log('导航到分类:', category.id, category.name)
  // 跳转到论坛首页，并传递分类参数
  router.push({
    path: '/forum',
    query: { category: category.id }
  })
}

// 组件挂载时加载数据
onMounted(() => {
  loadCategories()
})
</script>

<style lang="scss" scoped>
.forum-category-view {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.page-header {
  margin-bottom: 40px;
}

.header-content {
  text-align: center;
}

.page-title {
  font-size: 32px;
  font-weight: 700;
  color: #1e293b;
  margin: 0 0 8px 0;
}

.page-subtitle {
  font-size: 16px;
  color: #64748b;
  margin: 0;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 40px;
}

.category-card {
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 12px;
  overflow: hidden;
  
  &:hover {
    transform: translateY(-8px);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  }
}

.category-content {
  padding: 24px;
  text-align: center;
}

.category-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  border-radius: 20px;
  background: linear-gradient(135deg, #3b82f6, #60a5fa);
  color: white;
  margin: 0 auto 20px auto;
}

.category-info {
  .category-name {
    font-size: 20px;
    font-weight: 600;
    color: #1e293b;
    margin: 0 0 12px 0;
  }
  
  .category-description {
    font-size: 14px;
    color: #64748b;
    margin: 0 0 20px 0;
    line-height: 1.5;
    min-height: 42px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
}

.category-stats {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-top: 16px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: #64748b;
  
  .el-icon {
    font-size: 16px;
  }
}

.loading-container {
  padding: 40px;
}

.empty-container {
  padding: 60px 0;
  text-align: center;
}

@media (max-width: 768px) {
  .categories-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .category-content {
    padding: 20px;
  }
  
  .category-icon {
    width: 60px;
    height: 60px;
    margin-bottom: 16px;
  }
  
  .category-stats {
    gap: 16px;
  }
}
</style>
